package com.example.springboot.service.impl;

import com.example.springboot.controller.request.BaseRequest;
import com.example.springboot.entity.Category;

import com.example.springboot.mapper.CategoryMapper;
import com.example.springboot.service.ICategory;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CategoryService implements ICategory {

    @Resource
    CategoryMapper categoryMapper;

    //查询所有
    @Override
    public List<Category> list() {
        return categoryMapper.list();
    }

    //分页
    @Override
    public PageInfo<Category> page(BaseRequest baseRequest) {
        PageHelper.startPage(baseRequest.getPageNum(), baseRequest.getPageSize());
        List<Category> categories = categoryMapper.listByCondition(baseRequest);//自关联查询
        return new PageInfo<>(categories);
    }

    //更新
    @Override
    public void update(Category category) {
        Date date = new Date();
        category.setUpdateTime(date);
        categoryMapper.update(category);
    }


    //新增
    @Override
    public void save(Category category) {
        Date date = new Date();
        category.setUpdateTime(date);
        categoryMapper.save(category);
    }

    @Override
    public Category getById(Integer id) {
        return categoryMapper.getById(id);
    }

    //删除
    @Override
    public void deleteById(Integer id) {
        categoryMapper.deleteById(id);
    }

    @Override
    public Integer deleteByIds(List<Integer> ids) {
        return categoryMapper.deleteByIds(ids);
    }

}
